﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Sinacor.Infra.Service.Validation;
using Sinacor.Infra.Service.Common;
using Sinacor.Infra.Common.Exceptions;
using Sinacor.Infra.Service.ExceptionHandling;
using Sinacor.Infra.Service.Validation.BusinessRules;

namespace Sinacor.Servico.Bovespa.ManutencaoOrdens.BusinessLogic
{
    public class Justificativa : BaseBusinessLogic
    {
        #region Campos

        private int ERRO_PARAMETRONAOINFORMADO = 109381;
        private int NUM_ORDEM_NAOINFORMADO = 109334;
        private int DT_ORDEM_NAOINFORMADO = 109335;

        private int USUARIO_SEM_PERMISSAO_MORD = 109384;
        private int TAMANHO_JUSTIFICATIVA_INVALIDO = 109401;

        #endregion

        #region Construtor
        public Justificativa(){ }
        #endregion

        #region Metodos

        public BusinessEntity.Justificativa ObterJustificativa(BusinessEntity.Parameter.FiltroJustificativa request)
        {
            return new ResourceAccess.Justificativa().ObterJustificativa(request);
        }

        /// <summary>
		/// Verificar diagrama de atividade Justificativa.ConsultarJustificativas()
		/// </summary>
		/// <param name="request"></param>
		public List<BusinessEntity.Justificativa> ConsultarJustificativas(BusinessEntity.ChaveOrdem request)
        {
            if (request == null)
                throw new BusinessException(ERRO_PARAMETRONAOINFORMADO);

            if (request.NumeroOrdem == Int32.MinValue)
                throw new BusinessException(NUM_ORDEM_NAOINFORMADO);

            if (request.DataOrdem == DateTime.MinValue)
                throw new BusinessException(DT_ORDEM_NAOINFORMADO);

            ResourceAccess.Justificativa justificativa = new ResourceAccess.Justificativa();
            return justificativa.ConsultarJustificativas(request);

        }


        /// <summary>
        /// Alterar Justificativa
        /// </summary>
        /// <param name="request"></param>
        public void AlterarJustificativa(BusinessEntity.Justificativa request)
        {
            ResourceAccess.Justificativa raJustificativa = new ResourceAccess.Justificativa();

            if (Sinacor.Infra.Service.Security.Authorization.AuthorizationContext.Current.CheckUserAccess("MORD", "ORD"))
            {
                this.ValidarJustificativa(request);

                if (request.PendenteMensageria)
                {
                    raJustificativa.IncluirJustificativaMensageria(request);
                }
                else
                {
                    raJustificativa.AlterarJustificativa(request);
                }
            }
            else
            {
                throw new BusinessException(USUARIO_SEM_PERMISSAO_MORD);
            }
        }

        /// <summary>
        /// Alterar Justificativas
        /// </summary>
        /// <param name="request"></param>
        public void AlterarJustificativas(List<BusinessEntity.Justificativa> request)
        {
            foreach (BusinessEntity.Justificativa item in request)
            {
                this.AlterarJustificativa(item);
            }
        }

        /// <summary>
        /// Executar Transacoes
        /// </summary>
        /// <param name="incluiJustificativas"></param>
        /// <param name="alteraJustificativas"></param>
        public void ExecutarTransacoes(List<BusinessEntity.Justificativa> incluiJustificativas, List<BusinessEntity.Justificativa> alteraJustificativas)
        {
            this.IncluirJustificativas(incluiJustificativas);
            this.AlterarJustificativas(alteraJustificativas);
        }

        /// <summary>
        /// Incluir Justificativa
        /// </summary>
        /// <param name="request"></param>
        public void IncluirJustificativa(BusinessEntity.Justificativa request)
        {
            ResourceAccess.Justificativa raJustificativa = new ResourceAccess.Justificativa();
            
            if (Sinacor.Infra.Service.Security.Authorization.AuthorizationContext.Current.CheckUserAccess("MORD", "ORD"))
            {
                this.ValidarJustificativa(request);
                raJustificativa.IncluirJustificativa(request);
            }
            else
            {
                throw new BusinessException(USUARIO_SEM_PERMISSAO_MORD);
            }
        }

        /// <summary>
        /// Incluir Justificativas
        /// </summary>
        /// <param name="request"></param>
        public void IncluirJustificativas(List<BusinessEntity.Justificativa> request)
        {
            foreach (BusinessEntity.Justificativa item in request)
            {
                this.IncluirJustificativa(item);
            }
        }

        /// <summary>
        /// Validar Justificativa
        /// </summary>
        /// <param name="request"></param>
        private void ValidarJustificativa(BusinessEntity.Justificativa request)
        {
            SinacorValidation validation = new SinacorValidation();

            if (request != null)
                validation.Validate<BusinessEntity.Justificativa>(request);
            else
                validation.Validate<BusinessEntity.Justificativa>(new BusinessEntity.Justificativa());


            if (request.JustificativaOrdem.Length < 3)
                throw new BusinessException(TAMANHO_JUSTIFICATIVA_INVALIDO);
        }

        /// <summary>
        /// Inclui uma justificativa para a ordem porém a mesma fica pendente de alteração pelo usuário
        /// </summary>
        /// <param name="request"></param>
        public void IncluirJustificativaPendente(BusinessEntity.Justificativa request)
        {
            new ResourceAccess.Justificativa().IncluirJustificativaPendente(request);
        }

        #endregion
    }
}